package cn.xxxy.fourm.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import cn.xxxy.fourm.dao.UserDao;
import cn.xxxy.fourm.entity.Menu;
import cn.xxxy.fourm.entity.User;
import cn.xxxy.fourm.model.Node;
import cn.xxxy.fourm.model.UserDto;
import cn.xxxy.fourm.service.UserService;
@Service@Transactional
public class UserServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;

	@Override
	public List<UserDto> listPage(int page,int rows) {
		Pageable pageable = new PageRequest(page-1,rows);
		Page<User> list=userDao.findAll(pageable);
		List<UserDto> listDto=new ArrayList<>();
		for (User user : list) {
			System.out.println(user.getUname());
			UserDto userDto=new UserDto();
			userDto.setUid(user.getUid());
			userDto.setUname(user.getUname());
			userDto.setUpass(user.getUpass());
			userDto.setEmail(user.getEmail());
			userDto.setPhone(user.getPhone());
			userDto.setAddress(user.getAddress());
			userDto.setLevel(user.getLevel());
			userDto.setPhoto(user.getPhoto());
			userDto.setSign(user.getSign());
			userDto.setCreatetime(user.getCreatetime());
			userDto.setRid(user.getUserRole().getRid());
			userDto.setRname(user.getUserRole().getRname());
			userDto.setSid(user.getUserState().getSid());
			userDto.setSname(user.getUserState().getSname());
			listDto.add(userDto);
		}
		return listDto;
		
	}

	@Override
	public void deleteByUid(int uid) {
		userDao.deleteById(uid);;
		
	}

	public void update(User user) {
		userDao.save(user);
	}

	@Override
	public User updateByUid(int uid) {
		User user=userDao.getOne(uid);
		return user;
	}

	@Override
	public void addUser(User user) {
		userDao.save(user);
	}

	@Override
	public long count() {
		return userDao.count();
	}

	@Override
	public List<User> findByEmail(String email) {		
		return userDao.findByEmail(email);
	}

	@Override
	public List<User> findByEmailOrPassWord(String email, String password) {
		return userDao.findByEmailOrUpass(email, password);
	}

	@Override
	public User findByEmailAndUserName(String email, String userName) {
		return userDao.findByEmailAndUname(email, userName);
	}

	@Override
	public Page<User> list(Pageable pageable) {
		return userDao.findAll(pageable);
	}

	/*@Override
	public List<User> findByStartTimeBetween(String date1, String date2) {
		return userDao.findByStartTimeBetween(date1, date2);
	}

	@Override
	public List<User> findByAgeLessThan(int age) {
		return userDao.findByAgeLessThan(age);
	}

	@Override
	public List<User> findByUserNameLike(String username) {
		return userDao.findByUserNameLike(username);
	}

	@Override
	public List<User> findByUserNameStartingWith(String xx) {
		return userDao.findByUserNameStartingWith(xx);
	}*/

	/*@Override
	public List<Object[]> getUserMessage() {
		return userDao.getUserMessage();
	}

	@Override
	public List<Object[]> listUserOrder() {
		return userDao.listUserOrder();
	}*/

	@Override
	public Collection<User> listUsers() {
		return userDao.listUsers();
	}

	@Override
	public User login(String uname,String upass) {
		// TODO Auto-generated method stub
		return userDao.login(uname,upass);
	}

	@Override
	public void updateEmailAndPhoneAndAddressAndSign(String email, String phone, String address, String sign,Integer uid) {
		userDao.updateEmailAndPhoneAndAddressAndSign(email,phone,address,sign,uid);
	}

	

}
